package com.wujunshen.dumpling.common.exception;

import java.util.function.Function;

/**
 * The interface Throwing function.
 *
 * @param <T> the type parameter
 * @param <R> the type parameter
 * @param <E> the type parameter
 * @author drzqai
 */
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Throwable> {
  /**
   * Unchecked function.
   *
   * @param <T> the type parameter
   * @param <R> the type parameter
   * @param <E> the type parameter
   * @param f the f
   * @return the function
   */
  static <T, R, E extends Throwable> Function<T, R> unchecked(ThrowingFunction<T, R, E> f) {
    return t -> {
      try {
        return f.apply(t);
      } catch (Throwable e) {
        throw new RuntimeException(e);
      }
    };
  }

  /**
   * Apply r.
   *
   * @param t the t
   * @return the r
   * @throws E the e
   */
  R apply(T t) throws E;
}
